package com.yuandian.customer.perception.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yuandian.api.customer_perception.po.AreaMonitorParams;
import com.yuandian.api.customer_perception.vo.*;
import com.yuandian.bpm.common.core.util.R;
import com.yuandian.customer.perception.mapper.AreaMonitorMapper;
import com.yuandian.customer.perception.service.IAreaMonitorService;
import com.yuandian.enums.TableNameEnum;
import com.yuandian.po.MultipleTableInfo;
import com.yuandian.utils.PageDataInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * <p> 地市监控总览 service接口实现 </p>
 *
 * @Author: LinSheHi
 * @Date: 2021/8/16 10:03
 */
@Service
public class AreaMonitorServiceImpl implements IAreaMonitorService {

    @Autowired
    private AreaMonitorMapper areaMonitorMapper;


    /**
     * 查询全省总览
     *
     * @param params 查询全省总览请求参数
     * @return 通用返回对象
     */
    @Override
    public R<Object> queryAllAreaOverview(AreaMonitorParams params) {
        // 获取表信息集
		MultipleTableInfo info = new MultipleTableInfo();
		info.setTableName(TableNameEnum.ADS_BUSI_SYS_VISIT_TREND_M.getName());
		List<MultipleTableInfo> tableInfos = Collections.singletonList(info);

		AreaOverviewVo res = new AreaOverviewVo();
        List<Map<String, String>> areaList = areaMonitorMapper.queryAreaCount(params, tableInfos);
        int areaCount = 0;
        Integer hallCount = areaMonitorMapper.queryHallCount(params, tableInfos);
        Integer operatorCount = areaMonitorMapper.queryOperatorCount(params, tableInfos);
        for (Map<String, String> map : areaList) {
            if (map != null && !"000000".equals(map.get("areaCode"))) {
                areaCount += 1;
            }
        }

        res.setAreaCount(areaCount);
        res.setHallCount(hallCount == null ? 0 : hallCount);
        res.setOperatorCount(operatorCount == null ? 0 : operatorCount);

        return R.ok(res);
    }

    /**
     * 全地市受理量TOP5
     *
     * @param params 请求参数
     * @return 通用返回对象
     */
    @Override
    public R<Object> queryAllAreaCompleteCountTop(AreaMonitorParams params) {
        // 获取表信息集
		MultipleTableInfo info = new MultipleTableInfo();
		info.setTableName(TableNameEnum.ADS_BUSI_SYS_VISIT_TREND_M.getName());
		List<MultipleTableInfo> tableInfos = Collections.singletonList(info);

		List<AreaTopVo> res = areaMonitorMapper.queryAllAreaCompleteCountTop(params, tableInfos);

        return R.ok(res);
    }


    /**
     * 全地市告警量TOP5
     *
     * @param params 请求参数
     * @return 通用返回对象
     */
    @Override
    public R<Object> queryAllAreaAlarmCountTop(AreaMonitorParams params) {
		// 获取表信息集
		MultipleTableInfo info = new MultipleTableInfo();
		info.setTableName(TableNameEnum.KHGZ_BUSI_SYS_DETAIL_STAT.getName());
		List<MultipleTableInfo> tableInfos = Collections.singletonList(info);

		List<AreaTopVo> res = areaMonitorMapper.queryAllAreaAlarmCountTop(params, tableInfos);

        return R.ok(res);
    }


    /**
     * 全地市总览列表
     *
     * @param params 请求参数
     * @return 通用返回对象
     */
    @Override
    public R<Object> queryAllAreaOverviewList(AreaMonitorParams params) {
        Page<AreaOverviewListVo> page = new Page<>(params.getPageNum(), params.getPageSize());

		// 获取表信息集
		MultipleTableInfo info = new MultipleTableInfo();
		info.setTableName(TableNameEnum.ADS_BUSI_SYS_VISIT_TREND_M.getName());
		List<MultipleTableInfo> tableInfos = Collections.singletonList(info);

		List<AreaOverviewListVo> res = areaMonitorMapper.queryAllAreaOverviewList(page, params, tableInfos);
        for (AreaOverviewListVo re : res) {
            if (re.getAreaName() == null || re.getAreaName().equals("")){
                re.setAreaName("未知");
            }
        }
        return R.ok(new PageDataInfo(page, res));
    }


    /**
     * 办事处总览
     *
     * @param params 请求参数
     * @return 通用返回对象
     */
    @Override
    public R<Object> queryAllHallOverviewList(AreaMonitorParams params) {
        Page<HallOverviewListVo> page = new Page<>(params.getPageNum(), params.getPageSize());

		// 获取表信息集
		MultipleTableInfo info = new MultipleTableInfo();
		info.setTableName(TableNameEnum.ADS_BUSI_SYS_VISIT_TREND_M.getName());
		List<MultipleTableInfo> tableInfos = Collections.singletonList(info);

		List<HallOverviewListVo> res = areaMonitorMapper.queryAllHallOverviewList(page, params, tableInfos);
        return R.ok(new PageDataInfo(page, res));
    }


    /**
     * 操作员总览
     *
     * @param params 请求参数
     * @return 通用返回对象
     */
    @Override
    public R<Object> queryAllOperatorOverviewList(AreaMonitorParams params) {
        Page<OperatorOverviewListVo> page = new Page<>(params.getPageNum(), params.getPageSize());

		// 获取表信息集
		MultipleTableInfo info = new MultipleTableInfo();
		info.setTableName(TableNameEnum.ADS_BUSI_SYS_VISIT_TREND_M.getName());
		List<MultipleTableInfo> tableInfos = Collections.singletonList(info);

		List<OperatorOverviewListVo> res = areaMonitorMapper.queryAllOperatorOverviewList(page, params, tableInfos);
        return R.ok(new PageDataInfo(page, res));
    }


    /**
     * 全地市总览列表下载
     *
     * @param params 请求参数
     * @return 通用返回对象
     */
    //todo
    @Override
    public List<AreaOverviewListVo> queryAllAreaOverviewListExport(AreaMonitorParams params) {
		// 获取表信息集
		MultipleTableInfo info = new MultipleTableInfo();
		info.setTableName(TableNameEnum.KHGZ_BUSI_SYS_DETAIL_STAT.getName());
		List<MultipleTableInfo> tableInfos = Collections.singletonList(info);


		List<AreaOverviewListVo> res = areaMonitorMapper.queryAllAreaOverviewListExport(params, tableInfos);
        for (AreaOverviewListVo re : res) {
            if (re.getAreaName() == null || re.getAreaName().equals("")) {
                re.setAreaName("未知");
            }
        }
        return res;
    }


    /**
     * 办事处总览下载
     *
     * @param params 请求参数
     * @return 通用返回对象
     */
	@Override
    public List<HallOverviewListVo> queryAllHallOverviewListExport(AreaMonitorParams params) {
		// 获取表信息集
		MultipleTableInfo info = new MultipleTableInfo();
		info.setTableName(TableNameEnum.KHGZ_BUSI_SYS_DETAIL_STAT.getName());
		List<MultipleTableInfo> tableInfos = Collections.singletonList(info);

		return areaMonitorMapper.queryAllHallOverviewListExport(params, tableInfos);
    }


    /**
     * 操作员总览下载
     *
     * @param params 请求参数
     * @return 通用返回对象
     */
    //todo
    @Override
    public List<OperatorOverviewListVo> queryAllOperatorOverviewListExport(AreaMonitorParams params) {
		// 获取表信息集
		MultipleTableInfo info = new MultipleTableInfo();
		info.setTableName(TableNameEnum.KHGZ_BUSI_SYS_DETAIL_STAT.getName());
		List<MultipleTableInfo> tableInfos = Collections.singletonList(info);

		return areaMonitorMapper.queryAllOperatorOverviewListExport(params, tableInfos);
    }

}
